﻿using System;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using Movements.DataSet;

namespace Movements.Controls
{
    public partial class ProductListUserControl : XtraUserControl, IGridView, IList
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                SetupSupplierData();
                SetGridDataSource();
                _controller.ProductSaved += delegate { SetGridDataSource(); };
            }
        }

        #endregion

        public ProductListUserControl()
        {
            InitializeComponent();
            gridView.FocusedRowChanged += GridViewFocusedRowChanged;
            gridView.DoubleClick += GridViewDoubleClick;
            DevExpressHelper.SetColumnAsCurrency(colValor);
            DevExpressHelper.SetColumnAsCurrency(colFornecedorValor);
            DevExpressHelper.SetColumnAsNumericAndPaint(colStock);    
            DevExpressHelper.SetColumnAsNumericAndPaint(colFornecedorStock);    
        }
        
        #region Private Methods

        private void SetupSupplierData()
        {
            fornecedorDataSet.Clear();
            fornecedorDataSet.Merge(_controller.GetSuppliers());
            supplierRepositoryItemLookUpEdit.DataSource = fornecedorDataSet.Fornecedor;
            supplierRepositoryItemLookUpEdit.DisplayMember = fornecedorDataSet.Fornecedor.NomeColumn.ToString();
            supplierRepositoryItemLookUpEdit.ValueMember = fornecedorDataSet.Fornecedor.IdColumn.ToString();
            supplierRepositoryItemLookUpEdit.Columns.Clear();
            supplierRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Id", "Id", 70, FormatType.None, "", false, HorzAlignment.Near),
                new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near)});
        }

        private void SetGridDataSource()
        {
            var dataSet = _controller.GetProducts();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Produto.TableName;
            if (dataSet.Produto.Count > 0)
                gridView.FocusedRowHandle = 0;            
        }

        private void ShowProduct(long documentId)
        {
            var window = new ProductXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
        }

        #endregion

        void GridViewFocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var product = (ProdutoDataSet.ProdutoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            supplierGridControl.DataSource = null;
            if (product == null)
                return;
            produtoFornecedorDataSet = _controller.GetProduct(product.Id);
            supplierGridControl.DataSource = null;
            supplierGridControl.DataSource = produtoFornecedorDataSet;
            supplierGridControl.DataMember = produtoFornecedorDataSet.Fornecedor.TableName;
        }

        #region IGridView

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "ProductListUserControl"; }
        }

        #endregion

        public void AddDocument()
        {
            ShowProduct(-1);
        }

        public void DeleteDocument()
        {
            throw new NotImplementedException();
        }

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var product = (ProdutoDataSet.ProdutoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (product == null)
                return;
            ShowProduct(product.Id);
        }        
    }
}
